/**
 *  将险种标准格式转为树形结构
 * @param data 要转化的数据 格式是:Object
 * @param path 路径
 * @returns {*[]}
 */
export function dealRiskTree(data, path = '')  {

  let tree = [];
  for (let item in data) {
    let value = data[item];

    if (typeof value == 'object') {

      if (Array.isArray(value)) {

        let chirldrens = [];
        for (let [key, field] of value.entries()) {
          let paths = path ? `${path}.${item}` : item;

          let children = dealRiskTree(field, paths);

          chirldrens.push({
            label: '',
            field: '',
            name: '',
            path: paths,
            children: children
          })
        }

        let paths = path ? `${path}.${item}` : item;

        tree.push({
          label: item,
          field: item,
          name: '',
          path: paths,
          children: chirldrens
        });
      } else {
        let paths = path ? `${path}.${item}` : item;

        let children = dealRiskTree(value, paths);

        tree.push({
          label: item,
          field: item,
          name: '',
          path: paths,
          children: children
        });
      }
    } else {

      tree.push({
        label: `${item}-${value}`,
        name: value,
        field: item,
        path: path ? `${path}.${item}` : item
      })
    }
  }

  return tree;
}



/**
 * 将查询出来的列表数据转成tree格式
 * @param data 格式是数组
 */
export function list2FieldTree(data, type = 1) {
  if(!data) return [];

  let newData = data.map(item=>{
    let obj = {
      id: item.id,
      label: item.field,
      name: item.fieldName,
      field: item.field,
      path: '',
      parentId: item.parentId,
      originalId: item.originalId || item.id
    };

    return obj;
  });

  let treeData = [];
  if(type == 2) {
    treeData = dealExcPortTree(newData, 100, '', 'originalId');
  } else {
    treeData = dealExcPortTree(newData, 100, '');
  }

  return treeData;
}

/**
 *
 *
 * 按parentId来进行分类
 * @param data
 * @param pid
 * @returns {*[]}
 */
export function dealExcPortTree(data, pid = 100, path = '', identification='id') {
  const temp = [];
  for (const [key, item] of data.entries()) {
    if (item.parentId == pid) {
      let itemPath = path ? `${path}.${item.field}` : item.field;

      item.path   = itemPath;
      item.rowKey = item[identification];
      item.children = dealExcPortTree(data, item[identification], itemPath, identification);
      temp.push(item);
    }
  }
  return temp;
}




